<?php
/*
+--------------------------------------------------------------------------
|   AffiliStore 2
|   ========================================
|   Web: http://www.affilistore.com
|   Email: admin (at) affilistore (dot) com
|	License Type: AffiliStore 2 is NOT open source software and limitations apply 
|   Licence Info: Visit AffiliStore website and click on 'Licence'
+--------------------------------------------------------------------------
*/
// checks to see if password session is set
// if not redirects to ../admin/
if($_SESSION['loggedIn'] == 0 && !isset($adminPage)){
include('../includes/auth.inc.php');
}

// add a category
if (isset($_POST['cd_updated'])) {

	// check to see if details have been completed
	if (empty($_POST['addThisCategory'])) {
	$error = 1;

	// insert new category
	} else {

	$sql =  sprintf("INSERT INTO affiliSt_linkcategories (name, parent) values (%s, %s)",
			   quote_smart($_POST['addThisCategory']),
			   quote_smart($_POST['parentCategory']));
	mysql_query($sql, $databaseConnect) or die(mysql_error());
		
		// add one to parent subCategories counter
		if ($_POST['parentCategory'] != 'noparent') {
		$sql =  sprintf("UPDATE affiliSt_linkcategories SET subCategories = subCategories + 1 WHERE id = %s",
				   quote_smart($_POST['parentCategory']));
		mysql_query($sql, $databaseConnect) or die(mysql_error());
		}
	
	}
}


// delete a category
if (isset($_GET['delete'])) {
$sql =  sprintf("DELETE FROM affiliSt_linkcategories WHERE id = %s",
           quote_smart($_GET['delete']));
mysql_query($sql, $databaseConnect) or die(mysql_error());

	// subtract one from parent subCategories counter
	if (isset($_GET['p'])) {
	$sql =  sprintf("UPDATE affiliSt_linkcategories SET subCategories = subCategories - 1 WHERE id = %s",
				quote_smart($_GET['p']));
	mysql_query($sql, $databaseConnect) or die(mysql_error());
	}
}


// edit a category
if (isset($_POST['cd_edited'])) {

	// check to see if details have been completed
	if (empty($_POST['editThisCategory'])) {
	$error = 1;

	// insert new category
	} else {

	$sql =  sprintf("UPDATE affiliSt_linkcategories SET name = %s, parent = %s WHERE id = %s",
			   quote_smart($_POST['editThisCategory']),
			   quote_smart($_POST['parentCategory']),
			   quote_smart($_POST['editID']));
	mysql_query($sql, $databaseConnect) or die(mysql_error());
		
		if ($_POST['parentCategory'] != 'noparent' && $_POST['parentCategory'] != $_POST['oldParent']) {
		
		// add new
		$sql =  sprintf("UPDATE affiliSt_linkcategories SET subCategories = subCategories + 1 WHERE id = %s",
					quote_smart($_POST['parentCategory']));
		mysql_query($sql, $databaseConnect) or die(mysql_error());
		
		// minus old
		$sql =  sprintf("UPDATE affiliSt_linkcategories SET subCategories = subCategories - 1 WHERE id = %s",
					quote_smart($_POST['oldParent']));
		mysql_query($sql, $databaseConnect) or die(mysql_error());
		
		} else if  ($_POST['parentCategory'] == 'noparent') {
		
		// minus old
		$sql =  sprintf("UPDATE affiliSt_linkcategories SET subCategories = subCategories - 1 WHERE id = %s",
					quote_smart($_POST['oldParent']));
		mysql_query($sql, $databaseConnect) or die(mysql_error());
		
		}
	
	}
}



$getCatNames = mysql_query("SELECT * FROM affiliSt_linkcategories ORDER BY name ASC");
$catNames = mysql_fetch_assoc($getCatNames);

$getTopCat = mysql_query("SELECT * FROM affiliSt_linkcategories ORDER BY name ASC");
$topCat = mysql_fetch_assoc($getTopCat);












?>




<?php
// error report
if ($error == 1) {
echo '<fieldset><strong style="color:red">ERROR: Fields must not be empty</strong></fieldset>';
} else {
	// category added report
	if (isset($_POST['cd_updated'])) {
	echo '<fieldset><strong style="color:green">Category Added</strong></fieldset>';
	} 
	
	// category edited report
	if (isset($_POST['cd_edited'])) {
	echo '<fieldset><strong style="color:green">Category Edited</strong></fieldset>';
	}
}



?>



<?php
if (isset($_GET['edit'])) {


$sql =  sprintf("SELECT * FROM affiliSt_linkcategories WHERE id = %s",
           quote_smart($_GET['edit']));
$getEditCat = mysql_query($sql, $databaseConnect) or die(mysql_error());
$editCat = mysql_fetch_assoc($getEditCat);
?>

<fieldset style="border: 1px solid green;">
<legend>Edit Category</legend>
<form action="index.php?ad=ldc" method="post" name="editDetails">
<label for="editThisCat">Category Name: </label><input name="editThisCategory" type="text" id="editThisCat" value="<?php echo $editCat['name'] ?>" />
<label for="parentCat">Parent Category: </label>
<select name="parentCategory" id="parentCat" />
<option value="noparent">No Parent</option>
<?php 
// repeat cat names options
do { 
	if ($catNames['id'] !== NULL && $catNames['parent'] == 0 && $editCat['subCategories'] == 0 && $editCat['id'] != $catNames['id']) { ?>
	<option value="<?php echo $catNames['id'] ?>"
	<?php if ($editCat['parent'] == $catNames['id']) {
	echo ' selected="selected" ';
	}
	?>
	><?php echo $catNames['name'] ?></option>
<?php }
} while ($catNames = mysql_fetch_assoc($getCatNames)); ?>
</select>
<input type="hidden" name="editID" value="<?php echo $_GET['edit']; ?>" />
<input type="hidden" name="oldParent" value="<?php echo $_GET['p']; ?>" />
<input type="hidden" name="cd_edited" value="yes" />
<input value="Edit Category" type="submit" class="padSubmit" />
</form>
</fieldset>

<?php } else { ?>

<fieldset>
<legend>Add Category</legend>
<form action="index.php?ad=ldc" method="post" name="updateDetails">
<label for="addThisCat">Category Name: </label><input name="addThisCategory" type="text" id="addThisCat" />
<label for="parentCat">Parent Category: </label>
<select name="parentCategory" id="parentCat" />
<option value="noparent" selected="selected">No Parent</option>
<?php 
// repeat cat names options
do { 
	if ($catNames['id'] !== NULL && $catNames['parent'] == 0) { ?>
	<option value="<?php echo $catNames['id'] ?>"><?php echo $catNames['name'] ?></option>
<?php }
} while ($catNames = mysql_fetch_assoc($getCatNames)); ?>
</select>
<input type="hidden" name="cd_updated" value="yes" />
<input value="Add Category" type="submit" class="padSubmit" />
</form>
</fieldset>

<?php } ?>

<fieldset>
<legend>Current Categories</legend>


<?php 
// repeat category listings
do { 
	if ($topCat['id'] !== NULL && $topCat['parent'] == 0) {
	echo '<br /><strong>'.$topCat['name'].' ('.$topCat['counter'].')</strong>
	[<a href="index.php?ad=ldc&edit='.$topCat['id'].'">edit</a>] ';
		
		// delete if category has no links
		if ($topCat['counter'] == 0 && $topCat['subCategories'] == 0) {
		echo '[<a href="index.php?ad=ldc&delete='.$topCat['id'].'">delete</a>]<br />';
		} else {
		echo '[<span style="color:grey">delete</span>]<br />';
		}

		$getSubCat = mysql_query("SELECT * FROM affiliSt_linkcategories ORDER BY parent ASC, name ASC");
		$subCat = mysql_fetch_assoc($getSubCat);
	
		// repeat sub category listings
		do { 
			
			if ($subCat['parent'] == $topCat['id']) {
			echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;'.$subCat['name'].' ('.$subCat['counter'].')
			[<a href="index.php?ad=ldc&edit='.$subCat['id'].'&p='.$subCat['parent'].'">edit</a>] ';
		
				// delete if category has no links
				if ($subCat['counter'] == 0) {
				echo '[<a href="index.php?ad=ldc&delete='.$subCat['id'].'&p='.$subCat['parent'].'">delete</a>]<br />';
				} else {
				echo '[<span style="color:grey">delete</span>]<br />';
				}
			}
		} while ($subCat = mysql_fetch_assoc($getSubCat));
		
		
	}
} while ($topCat = mysql_fetch_assoc($getTopCat)); ?>
</fieldset>